package com.example.demo.utils;

import com.example.demo.model.dto.ResponseResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * 响应工具类（兼容现有ResponseResult）
 * 使用说明：
 * 1. 成功响应：ResponseUtil.success(data)
 * 2. 错误响应：ResponseUtil.fail(HttpStatus.BAD_REQUEST, "错误消息")
 * 3. 自定义HTTP状态：ResponseUtil.response(HttpStatus.CREATED, data)
 */
public class ResponseUtil {

    // ==================== 成功响应 ====================

    /**
     * 200 OK - 带数据
     */
    public static <T> ResponseEntity<ResponseResult<T>> success(T data) {
        return ResponseEntity.ok(ResponseResult.success(data));
    }

    /**
     * 200 OK - 无数据
     */
    public static ResponseEntity<ResponseResult<Void>> success() {
        return ResponseEntity.ok(ResponseResult.success());
    }

    /**
     * 201 Created - 带数据（用于创建资源）
     */
    public static <T> ResponseEntity<ResponseResult<T>> created(T data) {
        return ResponseEntity.status(HttpStatus.CREATED)
                .body(ResponseResult.success(data));
    }

    /**
     * 204 No Content - 无数据（用于删除成功等场景）
     * 注意：204状态码表示无内容，不应该返回响应体
     */
    public static ResponseEntity<Void> noContent() {
        return ResponseEntity.noContent().build();
    }

    // ==================== 错误响应 ====================

    /**
     * 自定义错误响应（使用HttpStatus）
     */
    public static <T> ResponseEntity<ResponseResult<T>> fail(
            HttpStatus status,
            String message) {
        return ResponseEntity.status(status)
                .body(ResponseResult.error(status.value(), message));
    }

    /**
     * 自定义错误响应（带错误数据）
     */
    public static <T> ResponseEntity<ResponseResult<T>> fail(
            HttpStatus status,
            String message,
            T errorData) {
        return ResponseEntity.status(status)
                .body(ResponseResult.error(status.value(), message, errorData));
    }

    // ==================== 快捷方法 ====================

    public static <T> ResponseEntity<ResponseResult<T>> badRequest(String message) {
        return fail(HttpStatus.BAD_REQUEST, message);
    }

    public static <T> ResponseEntity<ResponseResult<T>> unauthorized(String message) {
        return fail(HttpStatus.UNAUTHORIZED, message);
    }

    public static <T> ResponseEntity<ResponseResult<T>> forbidden(String message) {
        return fail(HttpStatus.FORBIDDEN, message);
    }

    public static <T> ResponseEntity<ResponseResult<T>> notFound(String message) {
        return fail(HttpStatus.NOT_FOUND, message);
    }

    public static <T> ResponseEntity<ResponseResult<T>> conflict(String message) {
        return fail(HttpStatus.CONFLICT, message);
    }

    public static <T> ResponseEntity<ResponseResult<T>> serverError(String message) {
        return fail(HttpStatus.INTERNAL_SERVER_ERROR, message);
    }

    // ==================== 高级用法 ====================

    /**
     * 自定义HTTP状态码和响应体
     */
    public static <T> ResponseEntity<ResponseResult<T>> response(
            HttpStatus status,
            int code,
            String message,
            T data) {
        // 使用构造器而非链式调用
        ResponseResult<T> result = new ResponseResult<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return ResponseEntity.status(status).body(result);
    }

    /**
     * 自定义HTTP状态码（使用成功响应体）
     */
    public static <T> ResponseEntity<ResponseResult<T>> response(
            HttpStatus status,
            T data) {
        return ResponseEntity.status(status)
                .body(ResponseResult.success(data));
    }
}